package com.daocao.hua.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.MySignAdapter;
import com.daocao.hua.api.callback.SignContract;
import com.daocao.hua.api.requestInfo.SignRequest;
import com.daocao.hua.constant.Const;
import com.daocao.hua.event.ChangeHomePagerEvent;
import com.daocao.hua.event.OnHomeEventMsg;
import com.daocao.hua.model.ResponseModel;
import com.daocao.hua.model.SignModel;
import com.daocao.hua.model.SignTaskModel;
import com.daocao.hua.util.Spans;
import com.daocao.hua.util.UI;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.CircularProgress;
import com.daocao.hua.view.TimeViewDialog;
import com.daocao.hua.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 2017/9/26.
 */

public class MySignActivity extends BaseActivity implements SignContract.View {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.btn_right)
    ImageView btnRight;
    @BindView(R.id.toolbar_sec)
    V19FrameLayout toolbarSec;
    @BindView(R.id.btn_sign)
    TextView btnSign;
    @BindView(R.id.layout_top)
    LinearLayout layoutTop;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.progressBar)
    CircularProgress loading;
    private Unbinder unbinder;
    private TimeViewDialog dialog;
    private SignRequest api;
    private Calendar cal;
    private int years;
    private int months;
    private MySignAdapter adapter;
    private boolean isFirst = false;
    private int signScore;

    //───
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign);
        unbinder = ButterKnife.bind(this);
        initTouMing();
        init();
    }

    private void showLoading(boolean b) {
        if (loading != null) {
            if (b)
                loading.setVisibility(View.VISIBLE);
            else
                loading.setVisibility(View.GONE);
        }
    }

    private void init() {
        adapter = new MySignAdapter(this);
        list.setAdapter(adapter);
        adapter.setonIdentityListener(new MySignAdapter.OnIdentityClick() {
            @Override
            public void onClick(int id) {
                switch (id) {
                    case 1:
                        api.gotoSign();
                        break;
                    case 2:
                    case 3:
                        Map<String, String> map = new HashMap<>();
                        map.put("type", Const.SendType.SWITCH_PAGE);
                        map.put("currentIndex", 1 + "");
                        EventBus.getDefault().post(new OnHomeEventMsg(map));
                        finish();
                        break;
                    case 4:
                        Util.startActivity(MySignActivity.this, V3InviteFriendsActivity.class);
                        finish();
                        break;
                    case 5:
                        EventBus.getDefault().post(new ChangeHomePagerEvent(1));
                        finish();
                        break;
                }
            }
        });
        cal = Calendar.getInstance();
        years = cal.get(Calendar.YEAR);
        months = (cal.get(Calendar.MONTH) + 1);
        api = SignRequest.create();
        api.attachView(this);
        api.getSignTask();
        dialog = new TimeViewDialog(this);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        adapter.setonIdentityListener(null);
        adapter = null;
        if (dialog != null) {
            dialog.dismiss();
            dialog = null;
        }
        api.detachView();
        api = null;
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_right, R.id.btn_sign, R.id.btn_rule})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_rule:
                Util.startActivity(this, PointRuleActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_right:
                if (isSign == 0) {
                    api.gotoSign();
                } else {
                    api.getSign(years + "", months + "");
                }
                break;
            case R.id.btn_sign:
                if (isSign == 0) {
                    api.gotoSign();
                } else {
//                    api.getSign(years + "", months + "");
                }
                break;
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        showLoading(false);
    }

    @Override
    public void onRequestStart() {
        showLoading(true);
    }

    @Override
    public void getSignSuccess(SignModel item) {        
        if (isFirst) {
            dialog.setTxtContent("签到成功，获得积分\n+" + signScore);
            isFirst = false;
        } else {
            dialog.setTxtContent("今天签过到了\n明天再来拿积分吧");
        }
        dialog.setTime((Integer[]) item.data.toArray(new Integer[item.data.size()]));
        dialog.show();
    }

    @Override
    public void gotoSignSuccess(ResponseModel item) {
        isFirst = true;
        api.getSignTask();
        api.getSign(years + "", months + "");
    }

    private int isSign = 0;

    @Override
    public void getSignTask(SignTaskModel item) {
        for (int i = 0; i < item.data.size(); i++) {
            if (item.data.get(i).id==1) {
                signScore = item.data.get(i).score;
                break;
            }
        }

        isSign = item.user.is_signin;
        adapter.update(item.data);
        if (isSign == 0) {
            return;
        }
        Spanned yudiString = Spans.with(this).font(item.user.point + "\n", getResources().getDimensionPixelSize(R.dimen.x70), 0xffff7a31).
                font("─────────\n\n", getResources().getDimensionPixelSize(R.dimen.x10), 0xffff7a31).
                font("积分", getResources().getDimensionPixelSize(R.dimen.x26), 0xb3ff7a31).
                build();
        btnSign.setText(yudiString);
    }
}
